package cn.jinchengcom.controller;

import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * @ClassName LogHandler
 * @Description TODO
 * @Author XuJincheng
 * @Date 2024-08-23
 */
@Aspect
@Component
@Slf4j
public class LogHandler {

    //execution(public * cn.jinchengcom.controller.*..*.*(..))
    @Pointcut("execution(public * cn.jinchengcom.controller..LogController.*(..))")
    public void point(){};



    /**
     * 外部调用统一处理
     * @param joinPoint
     * @return
     */
    @Before(value = "point()")
    public void around(JoinPoint joinPoint) {

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String url = request.getRequestURL().toString();
        Object[] args = joinPoint.getArgs();
        log.info("---------> url 【{}】 is start , param is {}",url, JSONArray.toJSONString(args));
    }


}
